package com.dk.service;

import cn.hutool.core.util.StrUtil;
import com.dk.mapper.LocationMapper;
import com.dk.page.PageResult;
import com.dk.pojo.Location;
import com.dk.pojo.LocationExample;
import com.dk.service.LocationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LocationServiceImpl implements LocationService {

    @Autowired
    private LocationMapper locationMapper;

    @Override
    public void insertlocation(Location location) {
        locationMapper.insert(location);
    }

    @Override
    public void deleteById(Integer id) {
        locationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAllById(List<String> ids) {
        locationMapper.deleteAllById(ids);
    }

    @Override
    public PageResult<Location> getAlllocation(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Location> locations = locationMapper.selectByExample(null);
        PageInfo<Location> pageInfo = new PageInfo<>(locations);
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Location> getlocationByName(String name) {
        LocationExample example = new LocationExample();
        if (!StrUtil.isEmpty(name) && !"".equals(name)) {
            example.createCriteria().andLNameLike("%" + name + "%");
        }
        List<Location> locations = locationMapper.selectByExample(example);
        return locations;
    }

    @Override
    public Location getById(Integer id) {
        return locationMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updatelocation(Location location) {
        locationMapper.updateByPrimaryKeySelective(location);
    }
}
